package com.briup.smartcity_group6.web.controller;

import com.briup.smartcity_group6.bean.basic.Category;
import com.briup.smartcity_group6.service.ICategoryService;
import com.briup.smartcity_group6.utils.Result;
import com.briup.smartcity_group6.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RequestMapping("/auth/category")
@Api(tags = "栏目模块")
@RestController
public class CategoryController {
    @Autowired
    private ICategoryService service;

    @ApiOperation("栏目删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "栏目id",required = true)
    })
    @DeleteMapping()
    public Result deleteById(Integer id){
        service.deleteById(id);
        return ResultUtil.success("操作成功");
    }

    @ApiOperation("查询所有栏目")
    @GetMapping("/all")
    public Result selectAll(){
        List<Category> categories=service.selectAll();
        return ResultUtil.success(categories);
    }

    @ApiOperation("分页查询所有栏目")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value = "pageNum",type = "int"),
            @ApiImplicitParam(name = "pageSize",value = "页大小",type = "int")
    })
    @GetMapping("page")
    public Result findByPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize){
        PageInfo<Category> pageInfo = service.finfAll(pageNum, pageSize);
        return ResultUtil.success(pageInfo);

    }

    @ApiOperation("批量删除栏目")
    @GetMapping("/batch")
    public Result deleteMany(List<Integer> ids){
        System.out.println(ids.toString());
        service.deleteMany(ids);
        return ResultUtil.success("操作成功");
    }

    @ApiOperation("保存栏目信息")
    @PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result addCategory(Category category){
        service.addCategory(category);
        return ResultUtil.success("操作成功");
    }

    @ApiOperation("更新栏目信息")
    @PutMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result updateCategory(Category category){
        //Category category1=new Category();
        //category1.setCategoryParentId(category.getCategoryParentId());
        //category1.setCategoryOrder(category.getCategoryOrder());
        //category1.setCategoryName(category.getCategoryName());
        //category1.setCategoryDescription(category.getCategoryDescription());
        service.updateCategory(category);
        return ResultUtil.success("操作成功");
    }


}
